package com.android.task5;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.widget.ImageView;
import android.widget.Toast;

/** Class for showing the images  */
public class ImageViewer extends ImageView implements Runnable {
	
	/** URL of the image to be displayed  */
	String URL;
	/** ArrayList with FlickrPhoto objects  */
	ArrayList<FlickrPhoto> flickrPhotos;
	/** ArrayList with PanoramioPhoto objects  */
	ArrayList<PanoramioPhoto> panoramioPhotos;
	
	InputStream is;
	
	int currentPhoto = 0;
	int numberOfFlickrPhotos;
	int numberOfPanoramioPhotos;
	boolean isFirst = true;
	
	FlickrManager flickrManager;
	
	boolean isFromPanoramio = true;
	
	public ImageViewer(Context context) {
		super(context);
	}
	
	public void setURL(String URL){
		this.URL = URL;
		Log.i("URL",this.URL);
	}
	/** Returns the URL of the next photo to be displayed  */
	private String getNextURL(){
		Log.i("URL","getNextURL");
		
		if(this.isFromPanoramio){
			return this.panoramioPhotos.get(this.currentPhoto).getDownloadUrl();
		}
		else{
			return this.flickrPhotos.get(this.currentPhoto).getDownloadUrl();
		}
	}
	
	public void setFlickrManager(FlickrManager flickrManager){
		this.flickrManager = flickrManager;
	}
	
	/** Method for displaying a image. Called by the ImageViewActivity.
	 *@param nextPhoto boolean indicating whether the next or the previous photo is to be displayed  */
	public void showImage(boolean nextPhoto){
		
		//If its the first time this ImageViewer is called, start with the first photo.
		if(isFirst){
			this.currentPhoto = 0;
			isFirst = false;
		}
		else{
			//If next photo is to be displayed
			if(nextPhoto){
				this.currentPhoto++;
				// If we have reached the end of the panoramio photos: Start displaying from FlickR
				if(this.isFromPanoramio && this.currentPhoto == this.numberOfPanoramioPhotos){
					this.isFromPanoramio = false;
					this.currentPhoto = 0;
				}
				//If we have reached the end of the FlickR photos: Start displaying from Panoramio.
				else if(!this.isFromPanoramio && this.currentPhoto == this.numberOfFlickrPhotos){
					this.isFromPanoramio = true;
					this.currentPhoto = 0;
				}
			}
			// If previous photo is to be displayed
			else{
				this.currentPhoto--;
				// If we have reached the end of the panoramio photos: Display the last Flickr
				// photo in the FlickrPhoto list.
				if(this.isFromPanoramio && this.currentPhoto == -1){
					this.isFromPanoramio = false;
					this.currentPhoto = this.numberOfFlickrPhotos-1;
				}
				// If we have reached the end of the FlickR photos: Display the last Flickr
				// photo in the Panoramio list.
				else if(!this.isFromPanoramio && this.currentPhoto == -1){
					this.isFromPanoramio = true;
					this.currentPhoto = this.numberOfPanoramioPhotos-1;
				}
			}
		}
		
		// Get the wanted image
		Drawable image = getImage();
		this.setImageDrawable(image);
		
		//Draw the image
		invalidate();
		
		// Create the toast message
		Context context = getContext();
		int duration = Toast.LENGTH_SHORT;
		String text = "";
		if(this.isFromPanoramio){
			PanoramioPhoto panoramioPhoto = this.panoramioPhotos.get(this.currentPhoto);
			text+=panoramioPhoto.getTitle();
			text+="\n";
			text += panoramioPhoto.getOwnerName();
		}
		else{
			FlickrPhoto flickrPhoto = this.flickrPhotos.get(this.currentPhoto);
			this.flickrManager.setInfoForPhoto(flickrPhoto);
			text += "Title: "+flickrPhoto.getTitle();
			text+="\n";
			text+= "Authors real name: "+flickrPhoto.getAuthorRealName();
			text+="\n";
			text+= "Authors user name: "+flickrPhoto.getAuthorUserName();
		}
		
		Toast toast = Toast.makeText(context, text, duration);
		toast.show();
	}
	/** Sets the Panoramio photos list */
	public void setPanoramioPhotos(ArrayList<PanoramioPhoto> panoramioPhotos){
		this.numberOfPanoramioPhotos = panoramioPhotos.size();
		this.panoramioPhotos = panoramioPhotos;
	}
	/** Sets the FlickR photos list */
	public void setFlickrPhotos(ArrayList<FlickrPhoto> flickrPhotos){
		this.numberOfFlickrPhotos = flickrPhotos.size();
		this.flickrPhotos = flickrPhotos;
		
	}
	/** Downloads the requested image using a separate thread. Waits until the InputStream
	 * is ready */
	private synchronized Drawable getImage(){
		new Thread(this).start();
		
		try {
			wait();
		} catch (InterruptedException e1) {
			e1.printStackTrace();
		}
	
		Drawable d = Drawable.createFromStream(this.is, "src");
		try {
			is.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return d;
		
	}
	/** Called by the underlying thread when the InputStream is ready. Notifies waiting threads. */
	private synchronized void setInputStream(InputStream is){
		this.is = is;
		notifyAll();
		
	}
	
	/**Downloads the actual photo and sets the input stream when it is ready. */
	@Override
	public void run() {
		
		try {
			Log.i("URL","Running");
			URL url = new URL(this.getNextURL());
			Log.i("URL","URL: "+url.toString());
			setInputStream((InputStream) url.getContent());
			
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	
		
	}
	

}
